<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['configuration:secondartservicetype:add']">新增</el-button>
      </el-col>
      <right-toolbar :search="false" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="servicepriceList" @selection-change="handleSelectionChange">
      <el-table-column label="服务类型" align="center" prop="serviceType" />
      <el-table-column label="方案类型" align="center" prop="programme">
        <template slot-scope="scope">
          {{
            [1, 2, 3, 4].includes(scope.row.serviceId)
              ? programmeTextOption[scope.row.programme]
              : "/"
          }}
        </template>
      </el-table-column>
      <el-table-column label="字数区间" align="center" prop="start">
        <template slot-scope="scope">
          {{
            [1, 2, 3, 4].includes(scope.row.serviceId)
              ? scope.row.start +
              (scope.row.type === 0 ? "-" + scope.row.end : "及以上")
              : "/"
          }}
        </template>
      </el-table-column>

      <el-table-column label="客户价格" align="center" prop="customerPrice">
        <template slot-scope="scope">
          {{ getDisplayText(scope.row) }}
        </template>
      </el-table-column>

      <el-table-column label="起步价格" align="center" prop="customerPrice">
        <template slot-scope="scope">
          {{ scope.row.startPrice }}
        </template>
      </el-table-column>

      <el-table-column label="返稿时间" align="center" prop="returnStartTime">
        <template slot-scope="scope">
          {{ getDayText1(scope.row) }}
        </template>
      </el-table-column>

      <!-- <el-table-column label="加急时间" align="center" prop="urgentStartTime">
        <template slot-scope="scope">
          {{ getDayText2(scope.row) }}
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="加收比例"
        align="center"
        prop="additionalChargeRatio"
      >
        <template slot-scope="scope">
          {{ getChargeRatio(scope.row) }}
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['configuration:serviceprice:edit']">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改服务价格配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务类型" prop="typeId">
          <el-input v-if="statusForm === 'edit'" v-model="form.serviceType" placeholder="请输入服务类型" disabled />
          <el-select v-if="statusForm === 'add'" v-model="form.typeId">
            <el-option v-for="item in addServiceOption" :key="item.id" :label="item.typeName" :value="item.id">
            </el-option></el-select>
        </el-form-item>

        <el-form-item label="方案类型" prop="programme" v-if="[1, 2].includes(form.typeId)">
          <el-select v-model="form.programme" placeholder="请选择方案类型">
            <el-option v-for="item in programmeOption" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="字数区间" prop="wordId" v-if="[1, 2].includes(form.typeId)">
          <el-select v-model="form.wordId" placeholder="请选择方案类型">
            <el-option v-for="item in wordConfOption" :key="item.id" :label="item.start + (item.type === 0 ? '-' + item.end : '及以上')
              " :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="计费方式" prop="billingMethod">
          <el-radio-group v-model="form.billingMethod">
            <el-radio :label="0" :disabled="[5, 9, 10].includes(form.typeId)">按字数</el-radio>
            <el-radio :label="1" :disabled="[1, 2, 3, 4, 6, 7, 8, 9, 10].includes(form.typeId) ||
              statusForm === 'add'
              ">按篇数</el-radio>
            <el-radio :label="2" :disabled="[1, 2, 3, 4, 5, 6, 8, 7].includes(form.typeId)
              ">客服报价</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="客户价格" prop="customerPrice" v-if="form.billingMethod != 2">
          <el-input v-model="form.customerPrice" placeholder="请输入客户价格">
            <template slot="append" v-if="form.billingMethod == 0">元/字</template>
            <template slot="append" v-if="form.billingMethod == 1">元/篇</template>
          </el-input>
        </el-form-item>

        <el-form-item label="起步价" prop="startPrice"
          v-if="form.billingMethod != 2 && [4,6,8].includes(form.typeId)">
          <el-input v-model="form.startPrice" placeholder="请输入客户价格">
            <template slot="append" v-if="form.billingMethod == 0">元/字</template>
            <template slot="append" v-if="form.billingMethod == 1">元/篇</template>
          </el-input>
        </el-form-item>

        <el-form-item v-if="form.typeId == 7" label="简介">
          <el-input v-model="form.description" placeholder="请输入简介">
          </el-input>
        </el-form-item>

        <el-form-item label="返稿时间" prop="returnStartTime">
          <el-row>
            <el-col :span="9">
              <el-input v-model="form.returnStartTime" placeholder="请输入天数" type="number" />
            </el-col>
            <el-col :span="2">
              <svg viewBox="0 0 12 5" width="0.8cm" height="1cm">
                <line x1="1" y1="2.5" x2="11" y2="2.5" stroke="#888" stroke-width="1" />
                <polygon points="11,2.5 9,1.5 9,3.5" fill="#888" />
              </svg>
            </el-col>
            <el-col :span="9">
              <el-input v-model="form.returnEndTime" placeholder="请输入天数" type="number" />
            </el-col>
            <el-col :span="4"> 个工作日 </el-col>
          </el-row>
        </el-form-item>

        <!-- <el-form-item label="是否加急" prop="urgent">
          <el-radio-group v-model="form.urgent">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item
          label="加急时间"
          prop="urgentStartTime"
          v-if="form.urgent == 1 && form.billingMethod != 2"
        >
          <el-row>
            <el-col :span="9">
              <el-input
                v-model="form.urgentStartTime"
                placeholder="请输入天数"
              />
            </el-col>
            <el-col :span="2">
              <svg viewBox="0 0 12 5" width="0.8cm" height="1cm">
                <line
                  x1="1"
                  y1="2.5"
                  x2="11"
                  y2="2.5"
                  stroke="#888"
                  stroke-width="1"
                />
                <polygon points="11,2.5 9,1.5 9,3.5" fill="#888" />
              </svg>
            </el-col>
            <el-col :span="9">
              <el-input v-model="form.urgentEndTime" placeholder="请输入天数" />
            </el-col>
            <el-col :span="4"> 个工作日 </el-col>
          </el-row>
        </el-form-item>

        <el-form-item
          label="加收比例"
          prop="additionalChargeRatio"
          v-if="form.urgent == 1 && form.billingMethod != 2"
        >
          <el-input
            v-model="form.additionalChargeRatio"
            placeholder="请输入加收比例"
          >
            <template slot="append">%</template>
          </el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listServiceprice,
  getServiceprice,
  delServiceprice,
  addServiceprice,
  updateServiceprice,
  getServicepriceType,
} from "@/api/configuration/serviceprice";
import { listWordConf } from "@/api/system/wordConf";
export default {
  name: "Serviceprice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务价格配置表格数据
      servicepriceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceId: null,
        serviceType: null,
        typeId: null,
        returnStartTime: null,
        returnEndTime: null,
        billingMethod: null,
        customerPrice: null,
        urgent: null,
        urgentStartTime: null,
        urgentEndTime: null,
        additionalChargeRatio: null,
        useCheckCoupon: null,
        havaTranslationProof: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceType: [
          { required: true, message: "服务名类型不能为空", trigger: "change" },
        ],
        programme: [
          { required: true, message: "请选择方案类型", trigger: "change" },
        ],
        typeId: [
          { required: true, message: "服务类型id不能为空", trigger: "blur" },
        ],
        wordId: [
          { required: true, message: "请选择字数区间", trigger: "change" },
        ],
        customerPrice: [
          { required: true, message: "客户价格", trigger: "blur" },
        ],
        returnStartTime: [
          { required: true, message: "返稿开始时间不能为空", trigger: "blur" },
        ],
        returnEndTime: [
          { required: true, message: "返稿结束时间不能为空", trigger: "blur" },
        ],
        billingMethod: [
          {
            required: true,
            message: "计费方式 0 按字数 1  按 篇数 2 客服报价不能为空",
            trigger: "blur",
          },
        ],
        urgent: [
          {
            required: true,
            message: "是否加急 0 否 1 是不能为空",
            trigger: "blur",
          },
        ],
        urgentStartTime: [
          { required: true, message: "加急开始时间不能为空", trigger: "blur" },
        ],
        urgentEndTime: [
          { required: true, message: "加急结束时间不能为空", trigger: "blur" },
        ],
        additionalChargeRatio: [
          { required: true, message: "加收比例不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
      },

      programmeTextOption: {
        0: "标准方案",
        1: "加快方案",
        2: "升级方案",
      },
      programmeOption: [
        {
          value: 0,
          label: "标准方案",
        },
        {
          value: 1,
          label: "加快方案",
        },
        {
          value: 2,
          label: "升级方案",
        },
      ],
      addServiceOption: [],
      //状态
      statusForm: "add", //add 新增 edit 编辑
      //字数区间
      wordConfOption: [],
    };
  },
  created() {
    this.getList();
    getServicepriceType().then((res) => {

      console.log("res", res);






      this.addServiceOption = res;
    });
    this.getWordConfList();
  },

  watch: {
    visible(newVal, oldVal) {
      if (!newVal) {
        this.reset();
      }
    },
  },
  methods: {
    getWordConfList() {
      listWordConf().then((res) => {
        this.wordConfOption = res.rows;
      });
    },

    /** 查询服务价格配置列表 */
    getList() {
      this.loading = true;
      listServiceprice(this.queryParams).then((response) => {
        this.servicepriceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getDisplayText(row) {
      switch (row.billingMethod) {
        case 0:
          return `${row.customerPrice}元/字`;
        case 1:
          return `${row.customerPrice}/篇`;
        case 2:
          return "客户报价";
        default:
          return "";
      }
    },
    getDayText1(row) {
      if (row.returnStartTime > 0 && row.returnEndTime > 0) {
        return `${row.returnStartTime}-${row.returnEndTime}个工作日`;
      } else {
        return "/";
      }
    },
    getDayText2(row) {
      if (row.urgentStartTime > 0 && row.urgentEndTime > 0) {
        return `${row.urgentStartTime}-${row.urgentEndTime}个工作日`;
      } else {
        return "/";
      }
    },
    getChargeRatio(row) {
      if (row.additionalChargeRatio > 0 && row.additionalChargeRatio > 0) {
        return `${row.additionalChargeRatio}%`;
      } else {
        return "/";
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serviceId: null,
        serviceType: null,
        typeId: null,
        returnStartTime: null,
        returnEndTime: null,
        billingMethod: 0,
        customerPrice: null,
        urgent: 0, //现在默认不加急 已废弃
        urgentStartTime: null, //已废弃
        urgentEndTime: null, //已废弃
        additionalChargeRatio: null,
        useCheckCoupon: null,
        createTime: null,
        updateTime: null,
        havaTranslationProof: null,
        programme: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.statusForm = "add";
      this.reset();
      this.open = true;
      this.title = "添加服务价格配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.statusForm = "edit";
      console.log(row);
      this.reset();
      const id = row.id || this.ids;
      getServiceprice(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务价格配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {



        if (valid) {
          if (this.statusForm === "edit") {
            updateServiceprice(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {

            for (let ele of this.addServiceOption) {

              if (ele.id == this.form.typeId) {
                this.form.serviceType = ele.typeName;
                break;
              }

            }

            console.log("this.form", this.form);

            this.form.serviceId = this.form.typeId;

            addServiceprice(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除服务价格配置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delServiceprice(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "configuration/serviceprice/export",
        {
          ...this.queryParams,
          ids: this.ids.length ? this.ids : undefined,
        },
        `serviceprice_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
